package net.sky.oa.service.system.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sky.oa.dao.system.SysLogingDao;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.model.SysLoging;
import net.sky.oa.service.system.SysLogingService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.util.AppUtil;

@Service
@Transactional
public class SysLogingServiceImpl extends BaseServiceImpl<SysLoging> implements SysLogingService {
	private SysLogingDao dao;
	
	private Long logingId;
	
	public Long getLogingId() {
        return logingId;
    }

    public void setLogingId(Long logingId){
    	this.logingId = logingId;
    }

	public SysLogingServiceImpl() {}
	
	public SysLogingServiceImpl(SysLogingDao dao) {
		super(dao);
		this.dao = dao;
	}

	public List<SysLoging> findAllLoging() {
		return this.dao.findAllLoging();
	}

	@Resource
	public void setDao(SysLogingDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}
	
	public void save(Long logingId){
		try{
			if(null != logingId){
				SysLoging entity = this.get(logingId);
				if(entity != null){
					entity.setLogouttime(new Date());
					super.save(entity);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String save(SysEmployee employee, String username, String ip, Integer type){
		String loginId = null;
		try{
			SysLoging entity = new SysLoging();
			entity.setLogintime(new Date());
			entity.setLoginname(username);
			entity.setType(type);
			entity.setLoginip(ip);
			entity.setSort(AppUtil.getIpType(ip));
			if(!type.equals(2) || employee != null){
				entity.setUser(employee);
			}
			super.save(entity);
			if(type.equals(1)){
				this.setLogingId(entity.getId());
			}
			loginId = String.valueOf(entity.getId());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return loginId;
	}
}